import { observable, action } from 'mobx';
import * as Api from '../../api/CenterApi';
import { PARAMS } from '../../util';
import { Comment,SearchAdvanced,ListViewPage } from 'weaver-mobile-page';

export default class RelateStore {
  @observable relatePage = 1;
  @observable searchType = 'all'; //不同tab需要的接口默认参数
  @observable myPostAdValue = ''; //高级搜索值受控
  @observable myPostComment = new Comment.Store({ getComment: this.getMyPostComment }); // 我的帖子--列表请求
  @observable waitApproveComment = new Comment.Store({ getComment: this.getWaitApproveComment }); // 待审批的帖子--列表请求
  @observable onMyPostSearch = new SearchAdvanced.Store({ route: '/center/publish', id: 'myPostSearch' });
  @observable myCommentLoading = true;
  @observable myCommentList = new ListViewPage.Store({ route: '/center/publish', id: 'myComment' });  // 我的评论列表

  @action setStoreState = (params) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    })
  }

  /**************************我的帖子*************************/
  @action('我的帖子列表请求')
  getMyPostComment = () => {
    const myPostSearchParams = this.onMyPostSearch.form.formParams || {};
    let defaultparams = {
      currentPage: this.relatePage++,
      searchType: 'all',
      ...myPostSearchParams,
    }
    return Api.getCoworkMineList(defaultparams).then(res => res)
  }

  @action('我的帖子高级搜索')
  getCoworkMineShareCondition = (params) => {
    this.onMyPostSearch.getData({
      url:`/api/cowork/mine/getCoworkMineShareCondition`,
      method: `GET`,
      params: {...params, ...PARAMS}
    })
  }

  @action('我的帖子搜索操作')
  myPostSearch = () => {
    this.relatePage = 1;
    this.myPostComment.getDatas();  // 刷新列表重新渲染数据
  }

  // 我的帖子 -- 高级搜素 -- 重置
  @action doMyPostReset = () => {
    this.onMyPostSearch.form.resetFields();
    this.myPostAdValue = '';
  }

  // 我的帖子 -- 高级搜索内外name统一
  @action updateMyPostAdvance = (value) => {
    this.myPostAdValue = value;
    this.onMyPostSearch.form.updateField('remark', value);
  }
  
  // 组件卸载执行操作
  clear = () => {
    this.onMyPostSearch.form.resetFields();  // 重置高级列表数据
    this.myPostAdValue = '';
    this.relatePage = 1;
    this.searchType = 'all';
  }

  /************************** 待审批的帖子 **************************/
  @action
  getWaitApproveComment = () => {
    const myPostSearchParams = this.onMyPostSearch.form.formParams || {};
    let defaultparams = {
      currentPage: this.relatePage++,
      searchType: 'approve',
      ...myPostSearchParams,
    }
    return Api.getCoworkMineList(defaultparams).then(res => res)
  }

  /**************************我的评论*************************/
  @action('我的评论列表')
  getMyCommentList = () => {
    const myPostSearchParams = this.onMyPostSearch.form.formParams || {};
    let defaultparams = {
      ...PARAMS,
      searchType:this.searchType,
      ...myPostSearchParams,
    }
    Api.getCoworkMineList(defaultparams).then(res => {
      const dataKey = res.sessionkey;
      if (dataKey) {
        this.dataKey = dataKey;
        this.myCommentList.getData({ dataKey }, ()=> this.myCommentLoading = false, true); // ture是否刷新用
        this.myCommentLoading = false;
      }
    })
  }

  // 我的评论 -- 组件挂载时处理
  @action doMyCommentDeleteMount = () => {
    this.onMyPostSearch.form.resetFields();
    this.myPostAdValue = '';
  }

  // 我的评论 -- 高级搜素 -- 重置
  @action doMyCommentReset = () => {
    this.onMyPostSearch.form.resetFields();
    this.myPostAdValue = '';
  }
}